﻿using System.Collections.Generic;
using UnityEngine;

namespace Gameplay.PVE
{
    public class AnimatorStage : StageBase
    {
        public struct AnimatorStageInfo
        {
            public float time;
            public string actionName;
        }
        
        public List<List<AnimatorStageInfo>> playerAnimList = new List<List<AnimatorStageInfo>>();
        public List<List<AnimatorStageInfo>> subPlayerAnimList = new List<List<AnimatorStageInfo>>();
      
        protected override void InitData()
        {
            isActionFinished = true;
            duration = 7.5f;
            playerAnimList.Clear();
            playerAnimList.Add(new List<AnimatorStageInfo>
            {
                new AnimatorStageInfo
                {
                    time = 0f,
                    actionName = "Attack",
                },
                new AnimatorStageInfo()
                {
                    time = 1.5f,
                    actionName = "Idle",
                },
                new AnimatorStageInfo()
                {
                    time = 4f,
                    actionName = "Cheer",
                }
            });
            playerAnimList.Add(new List<AnimatorStageInfo>
            {
                new AnimatorStageInfo()
                {
                    time = 4.5f,
                    actionName = "Cheer",
                }
            });
            playerAnimList.Add(new List<AnimatorStageInfo>
            {
                new AnimatorStageInfo()
                {
                    time = 4.5f,
                    actionName = "Cheer",
                }
            });
            subPlayerAnimList.Clear();
            subPlayerAnimList.Add(new List<AnimatorStageInfo>
            {
                new AnimatorStageInfo
                {
                    time = 1f,
                    actionName = "Cheer",
                },
                new AnimatorStageInfo
                {
                    time = 4.2f,
                    actionName = "Idle",
                }
            });

        }

        public override void Update()
        {
            base.Update();
            for (int i = 0; i < playerAnimList.Count; i++)
            {
                var list = playerAnimList[i];
                for (int j = 0; j < list.Count; j++)
                {
                    if (TimeManager.logicTime - startTime > list[j].time)
                    {
                        players[i].Play(list[j].actionName);
                        list.RemoveAt(j);
                        j--;
                    }
                }
            }
            
            for (int i = 0; i < subPlayerAnimList.Count; i++)
            {
                var list = subPlayerAnimList[i];
                for (int j = 0; j < list.Count; j++)
                {
                    if (TimeManager.logicTime - startTime > list[i].time)
                    {
                        subPlayers[i].Play(list[i].actionName);
                        list.RemoveAt(j);
                        j--;
                    }
                }
            }
        }
    }
}
